#include <alloca.h>
//
// Created by xsh on 2025/5/25.
//

#ifndef OPENSLTEST_SLENGINE_H
#define OPENSLTEST_SLENGINE_H

#include <SLES/OpenSLES_Android.h>
#include <vector>
#include <utility>
#include <map>
#include <functional>
#include <string.h>
//#include "include/instrument.h"
#include "../../../../common/include/Instrument.h"
#include "MidiMessage.h"

class SlEngine{
// 通道机架
class ChannelRack{
public:
    ChannelRack()=default;
   // ~ChannelRack();
    // 加载插件 通道机架 char* path libxxx.so 绝对路径
    void ChannelRackAdd(const char* path);
    void ChannelRackRemove();
    //allPlugins[] = ;
    //currentPlugin = ;
};
// 2025/6/3 混音器对象
class Mixer{};
/*struct ChannelRack{
    void* handle;
    Instrument* (*creator)();
    Instrument* instrument;
    std::map<std::string, std::function<void(Instrument*, const char*, jobject, JNIEnv*)>> eventHandlers; // 事件名到处理函数的映射
        };
std::vector<ChannelRack> loadedPlugins;*/

private:
    SLVolumeItf volumeItf;
    SLmillibel minLevel,maxLevel;
    SLEngineItf engineEngine;
    SLObjectItf engineObject;
    ChannelRack channelRack;
public:
    SLObjectItf playerObj;
    SLPlayItf playerPlay;
    constexpr static const double sampleRate = 48000.0;
    constexpr static const int samplesPerBuffer = 1024; //每个音频缓冲区中存放的音频样本数量
    Instrument* activeInstrument = nullptr; // 当前激活的乐器
    // do not remove
   // std::vector<std::pair<void*,Instrument*>>loadedPlugins;// 加载的插件列表
    void setVolume(float linearGain);
    float getVolume();
    void initEngine();
    void destoryEngine();
    void destoryPlugin(int plugin_index);
    // 设置当前乐器
    void setActiveInstrument(int index);
    // 发送事件到当前乐器
    void sendEvent(const char* eventName, jobject value, JNIEnv* env);
    static void engineQueueCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
    void play();
    void stop();

    void unloadPlugin(size_t index);
    // 2025/6/2
    void pause();
    // 只加载一个插件
    void testLoadOnePlugin(const char* libpath);
    //2025/6/4 midi消息队列
    std::vector<MidiMessage> midiQueue;

};



#endif //OPENSLTEST_SLENGINE_H
